package com.flyme.module.file.provider.service.impl;


import com.flyme.common.core.constants.CommonConstants;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.file.client.constants.FileConstants;
import com.flyme.module.file.client.entity.SysOffCourse;
import com.flyme.module.file.provider.mapper.SysOffCourseMapper;
import com.flyme.module.file.provider.service.SysOffCourseService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 线下课程管理接口实现
 */
@Service
public class SysOffCourseServiceImpl extends BaseServiceImpl<SysOffCourseMapper, SysOffCourse> implements SysOffCourseService {

    @Override
    public Boolean add(SysOffCourse offCourse) {
        offCourse.setCourseStatus(FileConstants.COURSE_PUB);
        offCourse.setApplyType(CommonConstants.DISABLED);
        return save(offCourse);
    }


    @Override
    public Boolean edit(SysOffCourse offCourse) {
        return saveOrUpdate(offCourse);
    }

    @Override
    public ResultBody delByIds(@RequestParam String[] ids) {
        this.remove(q().in("id", ids));
        return ResultBody.success("刪除成功");
    }

    /**
     * 首页查询线下课程
     */
    public List<EntityMap> selectLiveCourse() {
        PagerInfo pagerInfo = new PagerInfo();
        pagerInfo.setPageNumber(1);
        pagerInfo.setPageSize(5);
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("oc.id,courseTitle,courseImage,coursePrice,(select count(*) from sys_courseenroll ce where ce.targetId=oc.id) recordNum");
        cq.eq("courseStatus", 1);
        cq.eq("recomIndex", 1);
        List<EntityMap> list = (List<EntityMap>) pageList(cq).getRows();
        return list;
    }

    /**
     * 网站查询线下课程（分页）
     */
    public PqGrid selectofflineCourseWeb(PagerInfo pagerInfo) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
//        queryWrapper.select("oc.id,courseTitle,courseImage,coursePrice,courseBeginDate,user.avatar,user.userName");
        queryWrapper.select("oc.id,courseTitle,courseImage,coursePrice,courseBeginDate");
        queryWrapper.eq("courseStatus", 1);
//        queryWrapper.createAlias("user", "u");
        PqGrid pg = pageList(queryWrapper);
        return pg;
    }

    /**
     * 搜索课程
     */
    public PqGrid searchpageCourse(BigDecimal iosRatio, String keyWord, PagerInfo pagerInfo) {
        pagerInfo.setPageSize(10);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("oc.id,oc.courseTitle,oc.courseImage,oc.coursePrice,oc.courseContent,oc.courseAddress,oc.deadline,lecturer.lecturerName");
        queryWrapper.addSql("(round(oc.coursePrice*"+iosRatio+")) iosPrice");
        queryWrapper.like("oc.courseTitle", keyWord);
        queryWrapper.eq("oc.courseStatus", 1);
        queryWrapper.orderByAsc("courseOrder");
        queryWrapper.createAlias("lecturer", "lecturer");
        return  pageList(queryWrapper);
    }


    /**
     * 网站首页查询线下课程
     *
     * @return
     */
    public List<EntityMap> selectLiveCourseWeb(PagerInfo pagerInfo) {
//        PagerInfo pagerInfo = new PagerInfo();
//        pagerInfo.setPageNumber(1);
//        pagerInfo.setPageSize(5);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
//        queryWrapper.select("oc.id,courseTitle,courseImage,coursePrice,courseBeginDate,user.avatar lecturerImg,user.userName lecturerName");
        queryWrapper.select("oc.id,courseTitle,courseImage,coursePrice,courseBeginDate");
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("recomIndex", 1);
        queryWrapper.orderByDesc("deadline");
//        queryWrapper.createAlias("user", "u");
        List<EntityMap> list = (List<EntityMap>) pageList(queryWrapper).getRows();
        return list;
    }
}
